export default {
	data(){
		return {
			backList:[],//银行列表
			isAdd:true,//是否添加
			bank_account:'',//账户
			checked:false,//截止日期
			time:[],//筛选的时间
			search_form:{
				supplier:"",
				start_time:'',
				end_time:'',
				customer:''
			},
			sumTotal:0, //总金额
			newList:[],
			sortType:1,
			info:{
				bank_name:'',
				account_number:'',
				id:'',
				type:'',
				name:''
			}
		}
	},
	computed:{
		checkAmount(){
			// let sum = 0
			// for(let item of this.selectGoods){
			// 	if(this.ttype==1){
			// 		sum+=item.residue_money*item.goods_number
			// 	}else{
			// 		let money_put = item.code?item.residue_money_put:item.residue_money
			// 		sum+=money_put*item.goods_number
			// 	}
				
			// }
			// return sum
			
			
			let sum = 0
			for(let item of this.selectGoods){
				// if(item.is_return!=1){
				// 	sum+=item.price*item.goods_number
				// }
				sum+=item.price*Math.abs(item.goods_number) 
			}
			
			return sum.toFixed(2)
			
		},
		notPayTotal(){
			// let sum = 0
			// for(let item of this.list){
			// 	if(this.ttype==1){
			// 		sum+=item.residue_money*item.goods_number
			// 	}else{
			// 		let money_put = item.code?item.residue_money_put:item.residue_money
			// 		sum+=money_put*1
			// 	}
				
			// }
			// return sum
		
			let sum = 0
			for(let item of this.list){
				// if(item.is_return!=1){
				// 	sum+=item.price*item.goods_number
				// }
				sum+=item.price*Math.abs(item.goods_number) 
			}
			return sum.toFixed(2)
		},
		payTotal(){
			let sum = 0
			for(let item of this.list){
				// if(item.is_return!=1){
				// 	sum+=item.price*Math.abs(item.goods_number) 
				// }
				sum+=item.price*Math.abs(item.goods_number) 
				
			}
			return sum.toFixed(2)
		},
	},
	mounted(){
		let now = this.$utils.formatNowDate(2)
		this.buyer_date = now
		this.buyer_person = sessionStorage.getItem('user_name')
		this.lodeBackList()
	},
	methods:{
		compare2(obj1,obj2){
			var val1
			var val2
			if(this.ttype==1){
				val1 = obj1.buyer_date;
				val2 = obj2.buyer_date;
			}else{
				val1 = obj1.buyer_date_wholesaler?obj1.buyer_date_wholesaler:obj1.buyer_date;
				val2 = obj2.buyer_date_wholesaler?obj2.buyer_date_wholesaler:obj2.buyer_date;
			}
			    if (val1 > val2) {
			        return -1;
			    } else if (val1 < val2) {
			        return 1;
			    } else {
			        return 0;
			    }    
		},
		compare1(obj1,obj2){
			
			var val1 
			var val2
			if(this.ttype==1){
				val1 = obj1.buyer_date;
				val2 = obj2.buyer_date;
			}else{
				val1 = obj1.buyer_date_wholesaler?obj1.buyer_date_wholesaler:obj1.buyer_date;
				val2 = obj2.buyer_date_wholesaler?obj2.buyer_date_wholesaler:obj2.buyer_date;
			}
			
			    if (val1 < val2) {
			        return -1;
			    } else if (val1 > val2) {
			        return 1;
			    } else {
			        return 0;
			    }      
		},
		sortBuyerDate(){
			this.sortType==1?this.sortType=2:this.sortType=1
			
			if(this.sortType==1){
				this.list.sort(this.compare1)
				
			}else{
				this.list.sort(this.compare2)
			}
			
		},
		allpay(type){
			// type 1 全付 或 全收  2 全不付 或 全不收
			for(let item of this.list){
				if(type==1){
					item.one_pay = item.residue_money
				}else{
					item.one_pay = 0
				}
			}
		},
		//同步账户
		syncAccount(){
			if(!this.bank_account) return this.$message.error('请选择账户')
			for(let item of this.list){
				item.bank_account = this.bank_account
			}
		},
		//删除未选项目
		delItems(){
			this.toggleInverseAllSelection()
			this.list = this.selectGoods
		},
		//反选
		toggleInverseAllSelection(){
		  this.list.forEach((item) => {
		    this.$refs.multipleTable.toggleRowSelection(item);
		  });
		},
		lodeBackList(){
			this.$post('/baseInfo/getAccountList',{status:1},res=>{
				this.backList = res.data.data
			})
		},
		toPutPayInfo(row){
			// 1 未付款去采购入库 2未收款去快速批发
			let ttype = this.ttype
			//有code 是货款 没有是预收
			let code = row.code
			let code_arr = row.code_arr
			if(ttype==1){
				if(code || code_arr){
					if(code_arr){
						this.jump(`/buyerSys/buyerInfo?order_number=${row.order_number}`)
						// this.$router.push({
						// 	path:'/buyerSys/buyerInfo',
						// 	query:{
						// 		order_number:row.order_number
						// 	}
						// })
					}
					if(code){
						this.jump(`/buyerSys/buyerReturn?order_number=${row.order_number}`)
						// this.$router.push({
						// 	path:'/buyerSys/buyerReturn',
						// 	query:{
						// 		order_number:row.order_number
						// 	}
						// })
					}
				}else{
					this.jump(`/settlementSys/settlementPay?order_number=${row.order_number}`)
					// this.$router.push({
					// 	path:'/settlementSys/settlementPay',
					// 	query:{
					// 		order_number:row.order_number
					// 	}
					// })
				}
			}else{
				if(code || code_arr){
					if(code_arr){
						this.jump(`/wholesalerSys/wholesaler?order_number=${row.order_number}`)
						// this.$router.push({
						// 	path:'/wholesalerSys/wholesaler',
						// 	query:{
						// 		order_number:row.order_number
						// 	}
						// })
					}
					if(code){
						this.jump(`/wholesalerSys/wholesalerReturn?order_number=${row.order_number}`)
						// this.$router.push({
						// 	path:'/wholesalerSys/wholesalerReturn',
						// 	query:{
						// 		order_number:row.order_number
						// 	}
						// })
					}
					
				}else{
					this.jump(`/settlementSys/settlementPut?order_number=${row.order_number}`)
					// this.$router.push({
					// 	path:'/settlementSys/settlementPut',
					// 	query:{
					// 		order_number:row.order_number
					// 	}
					// })
				}
			}
		},
		getCustomerName(val){
			if(this.ttype==1){
				this.search_form.supplier = val
				this.lodeUserInfo(val)
			}else{
				this.search_form.customer = val
			}
		},
		lodeUserInfo(val){
			this.$post('/customer/lodeUserInfo',{supplier:val},res=>{
				let result = res.data
				if(result.code==200){
					this.info = result.data
					this.info.name = val.split('_')[0]
				}
			})
		},
		 copyText() {
			let payTotal = this.$utils.toggleInt(this.payTotal) 
let str = `${this.info.name} ${this.info.bank_name}
${this.info.account_number}
${this.bank_account} ${payTotal}`
		     let bool = this.$utils.exeCommandCopyText(str)
			 if(bool){
				 this.$message.success('复制成功')
			 }else{
				 this.$message.error('复制失败')
			 }
		},
		//上一个 下一个
		prevNext(tt){
			// tt 1上 2下
			let params = {
				order_number:this.$route.query.order_number || -1,
				type:this.ttype,
				handerType:tt
			}
			this.$post('/putpay/prevNextPayAmount',params,res=>{
				let result = res.data
				let order_number = result.data.order_number_new
				if(order_number){
					this.$router.replace({
						path:this.$route.path,
						query:{
							order_number:order_number
						}
					})
					this.lodePayPutOrder(order_number)
				}
			})
		},
		searchBtn(){
			this.$router.replace({
				path:this.$route.path,
				query:{}
			})
			this.lodeList()
		},
		lodeReturnData(){
			let customer = this.search_form.customer || this.search_form.supplier
			if(!customer) return this.$message.error(`请输入${this.ttype==1?'供应商':'客户'}`)
			let params = {
				customer:[customer],
				start_time:'',
				end_time:'',
				type:this.ttype,
				checked:this.checked
			}
			if(this.time.length==2){
				params.start_time = this.time[0]
				params.end_time = this.time[1]
			}
			
			this.$post('/putpay/getPayPutReturnData',params,res=>{
				let result = res.data.data
				console.log(this.ttype)
				let arr = []
				for(let item of result){
					let obj1 = {
						is_return:1,
						buyer_date:this.ttype==1?item.inst_store_date:item.out_store_date,
						code:item.code,
						order_number:this.ttype==1?item.inst_order_number:item.out_order_number,
						remark:this.ttype==1?item.inster_remark:item.out_remark,
						goods_name:item.goods_name,
						price:this.ttype==1?item.inst_store_price:item.out_store_price,
						goods_number:1,
						one_pay:this.ttype==1?item.inst_store_price:item.out_store_price,
						supplier:item.supplier,
						customer:item.customer,
						bank_account:'',
						remarks:'',
						id:item.id,
						cid:item.cid
					}
					if(obj1.order_number){
						arr.push(obj1)
					}
					
				}
				
				for(let item of result){
					let obj2 = {
						is_return:1,
						buyer_date:item.buyer_date,
						code:item.code,
						order_number:item.order_number,
						remark:item.remark,
						goods_name:item.goods_name,
						price:this.ttype==1?-item.price:-item.sales_price,
						goods_number:-1,
						one_pay:this.ttype==1?-item.price:-item.sales_price,
						supplier:item.supplier,
						customer:item.customer,
						bank_account:'',
						remarks:'',
						id:item.id,
						cid:item.cid
					}
					arr.push(obj2)
				}
				this.list.push(...arr)
			
			})
			
			
		},
		lodeList(){
			
			let customer = this.search_form.customer || this.search_form.supplier
			if(!customer) return this.$message.error(`请输入${this.ttype==1?'供应商':'客户'}`)
			let params = {
				customer:customer,
				start_time:'',
				end_time:'',
				type:this.ttype,
				checked:this.checked
			}
			if(this.time.length==2){
				params.start_time = this.time[0]
				params.end_time = this.time[1]
			}
			this.$post('/putpay/getPayPutData',params,res=>{
				let result = res.data
				if(result.code==200){
					let item = result.data
					if(item.length){
						let list = item[0].list
						if(list.length){
							
							// list = this.$utils.mergeDuplicates(list,this.ttype);
							let newArr = []
							for(let el of list){
								if(!el.bank_account){
									el.bank_account = ''
								}
								
								el.remarks = ''
								if(el.code_arr || el.code){
									if(el.code){
										if(this.ttype==2){
											el.price = -el.sales_price
											el.customer = el.supplier
										}else{
											el.price = -el.price
										}
									}else{
										if(this.ttype==2){
											el.price = el.sales_price
											el.customer = el.supplier
										}else{
											el.price = el.price
										}
									}
									// el.residue_money = el.residue_money?el.residue_money*el.goods_number:el.price*el.goods_number
									// el.residue_money_put = el.residue_money_put?el.residue_money_put*el.goods_number:el.price*el.goods_number
									el.residue_money = el.price*el.goods_number
									el.residue_money_put = el.price*el.goods_number
									
								}else{
									el.goods_number = 1
									el.price = -el.price
									el.residue_money = el.price*el.goods_number
									if(el.residue_money>0) el.residue_money = -el.residue_money
									el.one_pay = el.price*el.goods_number
								}
								if(this.ttype==1){
									el.one_pay = el.price*el.goods_number
								}else{
									if(el.code){
										el.one_pay = el.price*el.goods_number
									}else{
										el.one_pay = el.price*el.goods_number
									}
									
								}
								
								el.original_amount = el.price*el.goods_number
								newArr.push(el);
							}
							
							this.list = newArr
						
						}
					}else{
						this.list = []
					}
					
					// this.lodeReturnData()
					
				}
			})
			
		}
		
	}
}